<template>
    <div class="comment-management">
        <div style="display: flex;justify-content: flex-end;" fit>
            <!-- 搜索评论 -->
            <div style="display: flex;">
                <el-input v-model="filterKeyword" placeholder="请输入用户名/攻略名"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            </div>
        </div>
        <!-- 列表信息 -->
        <el-table :data="filteredComment" style="width: 100%;margin-top: 20px;" fit border>
            <el-table-column width="180" label="用户名" align="center">
                <template slot-scope="scope">
                    <p style="margin-left: 10px" v-if="scope.row.userId != null">{{ scope.row.username }}</p>
                    <p v-if="scope.row.username == null">个人信息未完善</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="头像" align="center">
                <template slot-scope="scope">
                    <img v-if="scope.row.userImage != null" style="width:180px" :src="scope.row.userImage" />
                    <p v-if="scope.row.userImage == null">个人信息未完善</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="攻略标题" align="center">
                <template slot-scope="scope">
                    <p>{{ scope.row.title }}</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="评论时间" align="center">
                <template slot-scope="scope">
                    <p>{{ new Date(scope.row.time).getFullYear() + "-"
                        + (new Date(scope.row.time).getMonth() + 1) + "-"
                        + new Date(scope.row.time).getDate() + " "
                        + new Date(scope.row.time).getHours() + ":"
                        + new Date(scope.row.time).getMinutes() + ":"
                        + new Date(scope.row.time).getSeconds()
                    }}</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="评论内容" align="center">
                <template slot-scope="scope">
                    <p>{{ scope.row.content }}</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="喜欢" align="center">
                <template slot-scope="scope">
                    <p>{{ scope.row.like }}</p>
                </template>
            </el-table-column>
            <el-table-column width="180" label="操作" align="center">
                <template slot-scope="{row}">
                    <el-button type="danger" size="small" @click="deleteComment(row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination @size-change="handlePageSizeChange" @current-change="handleCurrentPageChange"
            :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" :total="total"
            layout="total, sizes, prev, pager, next"></el-pagination>
    </div>
</template>
<script>
export default {
    data() {
        return {
            comment: [],// 获取的数据
            filteredComment: [],// 获取的数据
            filterKeyword: '',// 查找输入框的内容
            currentPage: 1, // 当前页
            pageSize: 10, // 当前页的条数
            total: 0 // 总条数
        }
    },
    methods: {
        // 获取评论信息
        filterComment() {
            const start = (this.currentPage - 1) * this.pageSize;
            const end = start + this.pageSize;
            this.$http
                .get('/getcomment')
                .then(res => {
                    console.log(res)
                    this.comment = res.data;
                    this.filteredComment = res.data.slice(start, end)
                    this.total = res.data.length
                })
        },
        // 搜索评论
        search() {
            const keyword = this.filterKeyword.trim().toLowerCase()
            if (keyword == '') {
                this.$message.error("请输入用户名或攻略名")
            } else {
                const start = (this.currentPage - 1) * this.pageSize;
                const end = start + this.pageSize;
                this.$http
                    .post('/selcomment', { keyword: keyword })
                    .then(res => {
                        if (res.data == undefined) {
                            this.filteredComment = res.data
                            this.$message.error(res.status)
                        } else {
                            this.filteredComment = res.data.slice(start, end)
                            this.total = res.data.length
                            this.$message.success(res.status)
                        }
                    })
            }
        },
        // 删除评论信息
        deleteComment(comment) {
            this.$confirm(`确定要删除评论 ${comment.content} 吗？`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.$http
                    .post('/delcomment', comment)
                    .then(res => {
                        this.$message.success(res.status)
                    })
                if (!this.filterKeyword) {
                    this.filterComment()
                } else {
                    this.search()
                }
            })
        },
        // 切换每页条数
        handlePageSizeChange(size) {
            this.pageSize = size
            this.currentPage = 1
            this.filterComment()
        },
        // 切换页码
        handleCurrentPageChange(page) {
            this.currentPage = page
            this.filterComment()
        },
    },
    mounted() {
        this.filterComment()
    },
}
</script>
